/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.cms.extra.tags;

import java.util.List;
import javax.jcr.Node;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.gidoo.cms.login.GidooCMSSession;


/**
 * A panel which shows a list of all tags belonging to a page. Will also
 * add links.
 * @author thomas
 */
public class TagListForPage extends Panel
{

  private int tagCount;

  public TagListForPage(String wicketID, GidooCMSSession session, Node page,
    final Class<? extends WebPage> tagListingPage)
  {
    super(wicketID);

    final List<String> tags = Helper.getTagsOfPage(page);

    tagCount = tags.size();

    ListView<String> lsvTagList = new ListView<String>("lsvTagList", tags)
    {

      @Override
      protected void populateItem(ListItem<String> item)
      {
        final String tag = item.getModelObject();
        
        Link<String> lnkTag = new Link<String>("lnkTag") {

          @Override
          public void onClick()
          {
            PageParameters pm = new PageParameters();
            pm.put("tag", tag);
            setResponsePage(tagListingPage, pm);
          }
        };

        lnkTag.add(new Label("lblTag", new Model<String>(tag)));
        item.add(lnkTag);

        Label lblDelimiter = new Label("lblDelimiter", new Model<String>(", "));
        item.add(lblDelimiter);
        lblDelimiter.setVisible(item.getIndex() < (tagCount-1));
      }
    };

    add(lsvTagList);
  }

  public int getTagCount()
  {
    return tagCount;
  }


}
